﻿$(function () {
    $.kish.productManagement = {};
    $.extend($.kish.productManagement, {
        initializing: function () {
            $.kish.productManagement.ListProducts();
            $.kish.productManagement.ListCategories();

            $(".AdminProdSearch_fn").click(function () {
                $.kish.productManagement.SearchProduct();
            });
        },

        //*************************//
        ListProducts: function (productToSearch) {
            $.ajax({
                url: '../Services/editProducts.svc/GetProducts',
                data: JSON.stringify({ prodToSearch: productToSearch }),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {

                    if (data['d'] != null) {
                        if (data['d'].length == 0) {
                            $.kish.message.getErrorMessage("15");
                        }
                        else {
                            var tableHeader = '<tr><th class="txtCenter">Image</th><th class="txtCenter width200">Name(EN)</th><th class="txtCenter width200">Name(Fa)</th><th class="txtCenter width80">Category</th><th class="txtCenter width80">Price</th><th>Edit</th><th>Delete</th></tr>';
                            $('.fn_Products').append(tableHeader);
                           for (var i = 0; i < data['d'].length; i++) {
                                var x = '<tr id=' + data['d'][i].ID + '><td ><a  class="highslide" href="' + data['d'][i].PhotoPath + '"> <img  class="width50 fn_prodImg " alt="No Image" title="click to see enlarge" src= "' + data['d'][i].PhotoPath + '"/></a></td> <td  class="width200  breakWord"> ' + data['d'][i].EnName + '</td><td class="width200  breakWord">' + data['d'][i].FaName + ' </td ><td class="width80">' + $.trim(data['d'][i].Category) + '</td><td class="width80">' + data['d'][i].Price + '</td><td><img alt="Edit" title="Edit" class="noBorder pointer fn_edit" src="../Images/ico/edit.gif" /></td><td><img alt="Delete" title="Delete" src="../Images/ico/delete.gif" class="noBorder pointer fn_del" /></td></tr>';
                                $('.fn_Products').append(x);
                            };

                            $('.fn_del').click(function () {
                                //  varc prodID = $(this).closest("tr").attr('id');
                                var thisRow = $(this).closest("tr");
                                $.kish.productManagement.Deleteproduct(thisRow);
                            });

                            $('.fn_edit').click(function () {
                                var prodID = $(this).closest("tr").attr('id');
                                window.location.replace("./UpdateProduct.aspx?ID=" + prodID);
                            });

                            $('a.highslide').click(function () {
                                return hs.expand(this)
                            });

                        }

                    }
                }
            });
        },


        //*************************//
        Deleteproduct: function (thisRow) {
            var prodID = thisRow.attr('id');
            $.kish.message.getConfirmationMessage(1, function () {

                $.ajax({
                    'type': 'POST',
                    'url': '../Services/EditProducts.svc/DeleteProduct',
                    'data': JSON.stringify({ "productID": prodID }),
                    'contentType': 'application/json; charset=utf-8',
                    'dataType': 'json',
                    'success': function (result) {
                        if (result['d'].IsSucceed) {
                            $(thisRow).remove();
                            //                            $.kish.productManagement.ListProducts();
                            $.kish.message.getUserMessage(result['d'].Message.Code);
                        }
                        else {
                            $.kish.message.getErrorMessage(result['d'].Message.Code);
                        }
                    },
                    'error': function () {
                        $.kish.message.alertError("Error loading message");
                    }
                });

            }, function () {
                // $this.show();
            });
        },

        //************************************//
        ListCategories: function () {
            $.ajax({
                url: '../Services/Admin.svc/GetCategories',
                data: '',
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {

                    if (data['d'] != null) {
                        if (data['d'].length == 0) {
                            $.kish.message.getErrorMessage("15")
                        }
                        else {

                            for (var i = 0; i < data['d'].length; i++) {
                                var x = '<option value="' + data['d'][i].ID + '">' + data['d'][i].EnName + '</option>';
                                $('.ddlCategory').append(x);
                            }
                        }
                    }
                }
            });
        },

        //*********************************************//
        SearchProduct: function () {
            var peoductToSearch = {};
            peoductToSearch.EnName = $.trim($(".txtProdEnName").val());
            peoductToSearch.FaName = $.trim($(".txtProdFaName").val());
            peoductToSearch.CategoryID = $(".ddlCategory").val();
            $('.fn_Products').html('');
            $.kish.productManagement.ListProducts(peoductToSearch);
        }

    });
    $.kish.productManagement.initializing();
});